// pages/speactor/ticket/ticket.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
    mt_id:'',
    rd_id:2,
    matchinfo:'',
    ticked_id:'',
    souce_id:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var matchid = options.mt_id;
    var roundid = options.rd_id;
    var souce_id = options.souce_id;
    var ticked_id = options.ticked_id;
    
    console.log(matchid);

    var scence = decodeURIComponent(options.scene);
    if(scence){
      var arrs = scence.split('=');
      if(arrs.length>1){
      matchid = arrs[0];//.split('=')[1];
      roundid = arrs[2];//.split('=')[1];
      souce_id = arrs[1];//.split('=')[1];
      this.setData({
        mt_id: matchid,
        rd_id: roundid,
        souce_id:souce_id,
      });
    }
     

      
    }

    //从分享过来的
    if (matchid) {

      this.setData({
        mt_id: matchid,
        rd_id: roundid,
        souce_id:souce_id,
        ticked_id:ticked_id,
      });
      this.getgetticketsinfo();
     
    } 
    else{

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (app.globalData.userInfo.openid && app.globalData.userInfo.session_key) {

      console.log(app.globalData.userInfo);
      this.setData({
        islogin: true
      })
    }

    if (!this.data.islogin && wx.getStorageSync('autoreg') == "1") {
      var that = this;
      app.wxlogin(app, function () {

        that.setData({
          islogin: true
        });
        that.onShow();
      });
      return;
    }

    if (!this.data.islogin) {
      this.login();
      return;
    }

    var userInfo = app.globalData.userInfo;
    if (userInfo != undefined && userInfo != null) {
      this.setData({
        userInfo: userInfo
      });
      // var obj = this.selectComponent("#register");
      // obj.filldata(userInfo.data);
    }
    
    
      return ;
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  
  login: function () {

    if (!app.checkislogin()) {
      wx.showModal({
        title: '请先登陆才能查看',
        confirmText: "确定",
        cancelText: "取消",
        success: function (res) {
          if (res.confirm) {
            console.log(res);
            wx.navigateTo({
              url: '/pages/login/login',
            })

            return;
          } else {
            wx.switchTab({
              url: '/pages/index/index',
            })
            return;
          }

        }
      })
    }


  },

  getgetticketsinfo:function(){
    var that = this;
    app.getgetticketsinfo(this.data.mt_id,this.data.rd_id,(this.data.ticked_id?this.data.ticked_id:''),this.data.souce_id,function(res){
      that.setData({matchinfo:res.data.data,
      ticked_id:res.data.data.ticked_id});
    },function(res){

    })
  },
  commit:function(){
        if(!this.data.matchinfo.ticketscodeno){
          wx.showToast({
            title: '无效的票号',
            duration:3000,
            icon:'error',
          })
          return;
        }
        var that = this;
        app.commitickets(this.data.mt_id,this.data.rd_id,this.data.ticked_id,this.data.matchinfo.ticketscodeno,
          this.data.souce_id,
          function(res){
            wx.showToast({
              title: '获取成功',
              icon:'success',
              duration:3000,
            })
            that.setData({ticked_id:res.data.data})
            wx.redirectTo({
              url: '/pages/speactor/ticket/ticketlist/index',
            })
            return;
          }, function(res){

      })
  }
})